package logic

import (
	"pingce/core/config"
	orm "pingce/core/db"
	"pingce/core/vars"
)

func GetConfig(group string, key string, ini_path ...string) string {
	db := orm.GetDb(ini_path...)
	sMap := config.GetDbConfig(ini_path...)
	prefix := sMap["prefix"]

	type Value struct {
		Value string `json:"value"`
	}

	var val Value

	db.Table(prefix+"conf").Select("value").Where("`group` = ? and `key` = ?", group, key).Find(&val)

	return val.Value
}

func GetConfigGroup(group string, ini_path ...string) []vars.MapStr {
	db := orm.GetDb(ini_path...)
	sMap := config.GetDbConfig(ini_path...)
	prefix := sMap["prefix"]

	type Value struct {
		Key   string `json:"key"`
		Value string `json:"value"`
	}

	var val []Value

	db.Table(prefix+"conf").Select("key", "value").Where("`group` = ?", group).Find(&val)

	data := make([]vars.MapStr, 0)
	for _, v := range val {
		data = append(data, vars.MapStr{
			"key":   v.Key,
			"value": v.Value,
		})
	}

	return data
}

func GetFilePath(fid int, ini_path ...string) string {
	db := orm.GetDb(ini_path...)
	sMap := config.GetDbConfig(ini_path...)
	prefix := sMap["prefix"]

	type Value struct {
		Path string `json:"path"`
	}

	var val Value

	db.Table(prefix+"files").Select("path").Where("`id` = ? ", fid).Find(&val)

	return val.Path
}
